// pages/my-order/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    params: {
      page: 1,
      pageSize: 10,
    },
    type: "",
    hasMore: false,
    tabActive: "0",
    tabs: [
      {
        label: "全部",
        value: "0",
      },
      {
        label: "待支付",
        value: "1",
      },
      {
        label: "待收货",
        value: "2",
      },
      {
        label: "已完成",
        value: "3",
      },
      {
        label: "取消",
        value: "4",
      },
      {
        label: "失效",
        value: "5",
      },
    ],
    orderList: [],
  },

  handleGetMore() {
    const { params, hasMore } = this.data;

    if (!hasMore) {
      params.page++;

      this.setData({
        params,
      });

      this.getData();
    }
  },

  getData() {
    const { tabActive, params, type } = this.data;

    wx.ajax
      .post(
        type == "7"
          ? "queryUserOrderList.action"
          : "queryMerchantOrderList.action",
        tabActive !== "0"
          ? { order_status: tabActive, ...params }
          : { ...params }
      )
      .then((res) => {
        // console.log(res);
        if (res.success) {
          const result = res.result;
          const originalList = this.data.orderList;

          this.setData({
            orderList: originalList.concat(result.pageList || []),
          });

          if (result.page === result.totalPages) {
            this.setData({
              hasMore: true,
            });
          }
        }
      });
  },

  handleTabClick(event) {
    const { index } = event.currentTarget.dataset;

    const { params } = this.data;

    params.page = 1;

    this.setData({
      tabActive: index.toString(),
      orderList: [],
      params,
    });

    this.getData();
  },

  handleToDetail(event) {
    const { id } = event.currentTarget.dataset;
    const { type } = this.data;

    wx.navigateTo({
      url: "/pages/my-order-detail/index?id=" + id + "&type=" + type,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.setData({
      type: options.type,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { params } = this.data;

    params.page = 1;

    this.setData({
      orderList: [],
      params,
    });

    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
